const db = require('../db/index')
const path = require('path')
const fs = require('fs')

// 查询赞赏人
exports.reware = (req, res) => {
    const sql = 'select * from ev_reware'
    db.query(sql, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        if (results.length > 0) {
            res.send({
                code: '200',
                data: results
            })
        }
    })
}

// 查询点赞总数
exports.loveTotle = (req, res) => {
    const sql = 'select * from love_total'
    db.query(sql, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        if (results.length >= 0) {
            res.send({
                code: 200,
                data: results[0].love_total
            })
        }
    })
}

// 增加点赞数
exports.addLoveNum = (req, res) => {
    const data = req.body
    const sql = 'update love_total set love_total=?'
    db.query(sql, data.num, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            code: 200,
            data: results
        })
    })
}

// 发送图片地址
exports.loveImgs = (req, res) => {
    const sql = "select upload_time,GROUP_CONCAT(imgUrl SEPARATOR ',') as imgUrl from imgs group by upload_time "
    db.query(sql, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        results.forEach(item => {
            item.imgUrl = item.imgUrl.split(',')
        });
        res.send({
            code: 200,
            data: results
        })
    })
}

// 上传图片
exports.uploadImg = (req, res) => {
    // 返回图片的地址 
    let file = req.files;
    //====此时，图片已经保存至我们的服务端了====
    let fileInfo = {}
    // 获取文件基本信息，封装好发送给前端
    fileInfo.type = file[0].mimetype;
    fileInfo.name = file[0].originalname;
    fileInfo.size = file[0].size;
    fileInfo.path = 'http://101.42.43.79/' + file[0].filename;
    const data = new Date();
    const year = data.getFullYear();
    const month = data.getMonth() + 1;
    const day = data.getDate();
    const res_data = `${year}-${month}-${day}`
    const sql = 'INSERT INTO imgs VALUES ( ?,? );'
    db.query(sql, [res_data, fileInfo.path], (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            code: 200,
            value: "图片上传成功",
            data: fileInfo
        })
    })
}

// 删除图片
exports.deleteImg = (req, res) => {
    const imgName = req.body.img
    const imgUrl = 'http://101.42.43.79/'+imgName
    const sql = 'delete from imgs where imgUrl=?'
    db.query(sql, imgUrl, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        fs.unlink(path.join(__dirname, '../public/imgs', imgName), (err) => {
            if (err) {
                res.send({
                    status: 1,
                    msg: '删除文件失败'
                })
            } else {
                res.send({
                    status: 0
                })
            }
        })
    })
}

// 上传视频
exports.uploadVideo = (req,res) => {
    // 返回视频的地址 
    let file = req.files;
    console.log(file)
    //====此时，视频已经保存至我们的服务端了====
    let fileInfo = {}
    // 获取文件基本信息，封装好发送给前端
    fileInfo.type = file[0].mimetype;
    fileInfo.name = file[0].originalname;
    fileInfo.size = file[0].size;
    fileInfo.path = 'http://101.42.43.79/' + file[0].filename;
    const data = new Date();
    const year = data.getFullYear();
    const month = data.getMonth() + 1;
    const day = data.getDate();
    const res_data = `${year}-${month}-${day}`
    const sql = 'INSERT INTO videos VALUES ( ?,? );'
    db.query(sql, [res_data, fileInfo.path], (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            code: 200,
            value: "图片上传成功",
            data: fileInfo
        })
    })
}

// 上传视频地址
exports.loveVideo = (req,res) => {
    const sql = "SELECT * FROM `videos` order by upload_time"
    db.query(sql, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        const ress = []
        results.forEach(item => {
            let result = [
                item
            ]
            ress.push(result)
        })
        res.send({
            code: 200,
            data: ress
        })
    })
}

// 删除视频
exports.deleteVideo = (req,res) => {
    const VideoName = req.body.video
    const src = 'http://101.42.43.79/'+VideoName
    const sql = 'delete from videos where src=?'
    db.query(sql, src, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        fs.unlink(path.join(__dirname, '../public/videos', VideoName), (err) => {
            if (err) {
                res.send({
                    status: 1,
                    msg: '删除文件失败'
                })
            } else {
                res.send({
                    status: 0
                })
            }
        })
    })
}

// 上穿评论（留言板）
exports.uploadMessage_lyb = (req,res) => {
    const Info = req.body.info
    const {username,label} = Info
    const data = new Date()
    const sql = 'insert into message_lyb values(?,?,?)'
    db.query(sql,[data,username,label],(err,results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            code:200,
            message:"上传留言成功",
            data
        })
    })
}

// 发送留言
exports.Message_lyb = (req,res) => {
    const sql = 'select * from message_lyb ORDER BY upload_time desc'
    db.query(sql,(err,results) => {
        if(err) {
            return res.cc(err.message)
        }
        res.send({
            code:200,
            data:results
        })
    })
}

// 上穿评论（关于我）
exports.uploadMessage_gyw = (req,res) => {
    const Info = req.body.info
    const {username,label} = Info
    const data = new Date()
    const sql = 'insert into message_gyw values(?,?,?)'
    db.query(sql,[data,username,label],(err,results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            code:200,
            message:"上传留言成功",
            data
        })
    })
}

// 发送留言(关于我)
exports.Message_gyw = (req,res) => {
    const sql = 'select * from message_gyw ORDER BY upload_time desc'
    db.query(sql,(err,results) => {
        if(err) {
            return res.cc(err.message)
        }
        res.send({
            code:200,
            data:results
        })
    })
}

// 删除留言
exports.deleteMessage_lyb = (req,res) => {
    const {username,label} = req.body
    const sql = 'delete from message_lyb where username=? and label=?'
    db.query(sql,[username,label],(err,results) => {
        if(err) {
            return res.cc(err.message)
        }
        res.send({
            code:200,
            message:"删除留言成功"
        })
    })
}

exports.deleteMessage_gyw = (req,res) => {
    const {username,label} = req.body
    const sql = 'delete from message_gyw where username=? and label=?'
    db.query(sql,[username,label],(err,results) => {
        if(err) {
            return res.cc(err.message)
        }
        res.send({
            code:200,
            message:"删除留言成功"
        })
    })
}

exports.uploadOnineWrite = (req,res) => {
    const {name,value} = req.body
    const sql = 'INSERT INTO online_writes(name,value) VALUES ( ?,? );'
    db.query(sql, [name, value], (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            code: 200,
            value: "文章上传成功",
            data: results
        })
    })
}

// 发送在线md文档的类型
exports.reqGet_o_Writes = (req, res) => {
    const sql = "select id,name from online_writes"
    db.query(sql, (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            code: 200,
            data: results
        })
    })
}

// 发送在线文档value
exports.reqGet_o_Inf = (req, res) => {
    const id = req.body.id
    const sql = "select value from online_writes where id = ?"
    db.query(sql,[id], (err, results) => {
        if (err) {
            return res.cc(err.message)
        }
        res.send({
            code: 200,
            data: results
        })
    })
}

// 删除在线文档
exports.delete_o_Write = (req,res) => {
    const {id} = req.body
    const sql = 'delete from online_writes where id = ?'
    db.query(sql,[id],(err,results) => {
        if(err) {
            return res.cc(err.message)
        }
        res.send({
            code:200,
            message:"删除留言成功"
        })
    })
}